<?php
namespace app\admin\controller;


use app\common\model\PersonnelNode;
use app\common\model\PersonnelRoleNodeAccess;
use think\Session;

class Backend extends \app\common\controller\Auth
{
    protected $isLogin = false; //判断是否登陆
    protected $uuid; //登陆后的UUID
    protected $config_list=[];

    //權限跳过检验的节点
    protected $index_array=[
        'Index'=>[
            'index'=>true,
            'login'=>true,
            'logout'=>true,
        ],
    ];

    protected $log_string;
    protected $member_info;
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        if ($this->checkLoginGlobal()) {
            $this->isLogin = true;
        }
        //检测是否登陆
        $controller = strtolower($this->request->controller());
        $action     = strtolower($this->request->action());
        if( $controller == 'index' && $action == 'login'){
            return false;
        }else{
            if(!$this->isLogin) {
                $this->error('请先登录','index/login');
                $this->redirect('index/login');
            }
        }

        //检测登陆权限
        $auth =$this->checkNodeAuth();
        //dump($auth);exit;
        if($auth!=true || $auth==false || is_string($auth)){
        // dump($this->log_string);
            $this->error($this->log_string,'index/index');
        }
        $this->member_info = Session::get('member_info','Global');
    }

    /**
     * 获取当前访问节点
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return bool
     */
    protected function checkNodeAuth()
    {
        if ($this->checkIsAdmin()) {
            return true;
        } else {
            $request = $this->request;
            if ($this->checkIsIndex($request->controller(), $request->action())) {
                return true;
            }
            //检测权限// 当前模块名
            $node = new PersonnelNode();
            //跳过登录系列的检测以及主页权限
            $node_info = $node->getNodeInfo($request->module(), $request->controller(), $request->action());
            if (empty($node_info)) {
                $this->log_string='此页面访问权限未开放，请联系管理员';
                return false;
            }
            if ($node_info['auth_grade'] > 0) {
                return $this->checkUserNodeAuthByNodeGuId($node_info['guid']);
            }
            return true;
        }
    }
    /**
     * 检测节点是否可以默认登录
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $controller
     * @param $action
     * @return bool
     */
    protected function checkIsIndex($controller,$action)
    {
        return isset($this->index_array[$controller][$action])? true : false;
    }
    /**
     * 判断用户是否有节点权限
     * Power: Mikkle
     * Email：776329498@qq.com
     * @param $Guid
     * @return bool
     */
    protected function checkUserNodeAuthByNodeGuId($Guid)
    {
        $member = $this->member_info;//dump($member);
        $node_list =[];
        if (Session::has("role_node_list_{$Guid}")){
            $node_list=Session::has("role_node_list_{$Guid}");
        }else{
            $model = new PersonnelRoleNodeAccess();
            $node_list = $model->getRoleMenuList($member['role_id'],1);
        }//dump($Guid);dump($node_list);
        if (!in_array($Guid, $node_list)) {
            $this->log_string="你没有权限，请联系系统管理员";
            return false;
        }else{
            return true;
        }
    }
    /**
     * 检测是否是管理员
     * Power: Mikkle
     * Email：776329498@qq.com
     * @return bool
     */
    protected function checkIsAdmin()
    {
        if (Session::has('is_admin')) {
            return true;
        } else {
            return false;
        }
    }









}
